'use strict'

module.exports = app => {
  return async (ctx, next) => {
    const { query } = ctx.socket.handshake
    const nsp = ctx.app.io.of('/')

    let name
    if (query.identity === 'company') {
      name = query.company_id
    } else {
      name = 'xh'
    }
    ctx.socket.join(name)
    const socketsRoom = nsp.to(name)
    socketsRoom.emit('online', query)
    await next()
  }
}
